﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sigil.Impl;

namespace Sigil
{
	/// <summary>
	/// Provides a way to lookup labels declared with an emit.
	/// </summary>
	public class LabelLookup
	{
		/// <summary>
		/// Returns the label with the given name.
		///
		/// Throws KeyNotFoundException if no label by that name is found".
		/// </summary>
		public Label this[String name]
		{
			get
			{
				if (!InnerLookup.ContainsKey(name))
				{
					throw new KeyNotFoundException("No label with name '" + name + "' found");
				}

				return InnerLookup[name];
			}
		}

		/// <summary>
		/// Returns the number of labels declared
		/// </summary>
		public Int32 Count { get { return _Names.Count; } }

		private List<String> _Names { get { return InnerLookup.Keys.Where(k => !k.StartsWith("__")).ToList(); } }

		/// <summary>
		/// Returns the names of all the declared labels
		/// </summary>
		public IEnumerable<String> Names { get { return _Names; } }

		private Dictionary<String, Label> InnerLookup;

		internal LabelLookup(Dictionary<String, Label> innerLookup)
		{
			InnerLookup = innerLookup;
		}
	}
}